package com.boxin.ims.modules.wechat.busi;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.sf.json.JSONObject;

import com.boxin.ims.modules.wechat.entity.Region;
import com.boxin.ims.modules.wechat.entity.TextMessage;
import com.boxin.ims.modules.wechat.entity.WeChat;
import com.boxin.ims.modules.wechat.entity.Weather;
import com.boxin.ims.modules.wechat.service.RegionService;
import com.boxin.ims.modules.wechat.utils.WechatUtils;
import com.thinkgem.jeesite.modules.sys.entity.User;

/**
 * @author Jakemanse
 * @date 2013-9-23
 * @function <p>天气预报处理器 </p>
 */
@Service
public class WeatherProcessor extends BusinessProcessor{

	
	@Autowired
	RegionService regionService;
	
	public WeatherProcessor(){
		super();
	}
	
	
	
	public WeatherProcessor(WeChat weChat,String openId,HttpServletRequest request,HttpServletResponse response){
		super(weChat, openId, request, response);
	}
	
	
	
	
	@Override
	public ProcessMessage process() {
		String reply = "";
		
		Region region = new Region();
		String upcon = textMessage.getContent();
		region.setName(upcon.replaceAll("天气", "").trim());
		List<Region> regions = 	regionService.find(region);
		reply = "未找到【"+textMessage.getContent()+"】的天气信息.";
		if(regions !=null && regions.size()>0){//打到了对应的天气的信息
			region = regions.get(0);
			String strjson = WechatUtils.getWeatherJson(region.getCode());
			JSONObject json  = JSONObject.fromObject(strjson); //object.getString("weatherinfo")
			JSONObject jsonw = JSONObject.fromObject(json.getString("weatherinfo"));
			Weather weather = WechatUtils.convertJsonToWeather(  jsonw);
			reply = weather.toMailWeatherString();
			writeToUser(reply);			//回复信息
			printReplyLogger(reply);
			return null;
		} 
		
		writeToUser(reply);				//回复款找到的信息
		printReplyLogger(reply);
		
		return null;
	}



	@Override
	public boolean isNeedBeMember() {
		return false;
	}

}
